package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.constant.MessageConst;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController{

    @Reference
    RoleService roleService;
    @Reference
    private MenuService menuService;


    @RequestMapping("/showMenu")
    public Result showMenu(String username) {
        log.debug("showMenu方法------>参数username:" + username);
        List<Menu> menuList = roleService.findRoleByUsername(username);
        log.debug("获取菜单集合menuList:" + menuList);
        return new Result(true, "查询菜单成功", menuList);

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,String> menuMap){
        log.debug("controller:{"+menuMap+"}");
        menuService.add(menuMap);
        log.debug("权限数据添加成功！！");
        return new Result(true, MessageConst.ADD_MENU_SUCCESS);
    }

    /**
     * 分页查询
     *  1.调用Service获取分页结果数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("controller{"+queryPageBean+"}");
        List<Menu> menuResult = menuService.findByPage(queryPageBean);
        log.debug("菜单数据查询成功!!");
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menuResult);
    }

    @RequestMapping("/findParentName")
    public Result findParentName(){
        log.debug("MenuController-findAll");
        List<Map<String,String>> list = menuService.findParentName();
        log.debug("父菜单数据查询成功!!");
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, list);
    }

    //删除
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("controller -- delete: {"+id+"}");
        menuService.delById(id);
        log.debug("菜单数据删除成功!!");
        return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("controller -- findById: {"+id+"}");
        List<Map<String,String>> list = menuService.findById(id);
        log.debug("菜单数据id查询：{"+ list +"}");
        return  new Result(true, MessageConst.QUERY_PERMISSON_SUCCESS,list);

    }

    @RequestMapping("/findMenuById")
    public Result findMenuById(Integer id){
        log.debug("controller -- findMenuById: {"+id+"}");
        Menu menu = menuService.findMenuById(id);
        log.debug("菜单数据id查询：{"+ menu +"}");
        return  new Result(true, MessageConst.QUERY_PERMISSON_SUCCESS,menu);

    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        log.debug("controller -- edit: {"+menu+"}");
        menuService.edit(menu);
        log.debug("菜单数据编辑成功！！！");
        return new Result(true,MessageConst.EDIT_MENU_SUCCESS);
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("MenuController:findAll: " );
        List<Menu> menuList = menuService.findAll();
        log.debug("检查组查询成功！！！");
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,menuList);
    }
}
